<?php

namespace app\beautiful\model\order;

use app\common\model\CommonModel;

use app\beautiful\model\member\MemberModel;



class StoreOrderModel extends CommonModel{

    protected  $name = 'beautiful_store_order';

    protected  $pk   = 'store_order_id';



    protected $type=[

        'goods_gift_giving'=>'array',

        'pay_info'         => 'array'  

    ];

    public function member()

    {

        return $this->hasOne(MemberModel::class,'member_id','member_id');

    }



    public function getPayTimeFormatAttr($value,$data){

        return $data['pay_time'] ? date('Y-m-d H:i:s',$data['pay_time']) : '-';

    }

    

    public function getOrderTimeAttr($value,$data){

        return date('Y-m-d H:i',$data['add_time']);

    }

    

    public function getDjsTimeAttr($value,$data){

        $t = time();

        $djs = $data['add_time'] + 900 - $t; //15分钟有效

        return  $djs > 0 ?  $djs : 0;

    }

     public function getPayTypeMeanAttr($value,$data)

    {

        $type_means=[

            'weixin'=>'微信支付',

            'money'=>'余额支付',

            'offline' => '线下补单',

        ];

        return $type_means[$data['pay_type']] ?? '-';

    }



    public function getStatusMeansAttr( $value,$data)

    {

        $status_means=[

            1=>'待付款',

            4=>'待自提',

            8=>'已完成',

            -1=>'已取消',

        ];

        return $status_means[$data['status']] ?? '-';

    }



    public function getGiftTotalPriceAttr($value,$data)

    {

        $price=0;

        $gift_giving=json_decode($data['goods_gift_giving'],true);

        if(!empty($gift_giving)){

            foreach ($gift_giving as $k =>$v){

                $price=$price+round(($v['price']*100*$v['num'])/100,2);

            }

        }

        return $price;

    }



    public function getRemainNumAttr($value,$data)

    {

        $remain_num=$data['goods_num']-$data['write_off_num'];

        return $remain_num>0 ? $remain_num : 0;

    }

}



